package com.yidu.utils.ExportWordTools;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

/**
 * @描述：导出word文档
 * @ClassName: wenjian
 * @backage: PACKAGE_NAME
 * @Date: 2019/11/29 9:08
 * @Author:胡立
 **/
public class ExportWord {

    Logger logger = LoggerFactory.getLogger(getClass());
    private XWPFHelperTable xwpfHelperTable = null;
    private XWPFHelper xwpfHelper = null;
    private TextStyle textStyle;
    public ExportWord() {
        xwpfHelperTable = new XWPFHelperTable();
        xwpfHelper = new XWPFHelper();

    }
    /**
     * 创建好文档的基本 标题，表格  段落等部分
     * @return
     * @Author Huangxiaocong 2018年12月16日
     */
    public XWPFDocument createXWPFDocument() {
        XWPFDocument doc = new XWPFDocument();
        createTitleParagraph(doc);
        createTableParagraph(doc, 16, 4);

        return doc;
    }
    /**
     * 创建表格的标题样式
     * @param document
     * @Author Huangxiaocong 2018年12月16日 下午5:28:38
     */
    public void createTitleParagraph(XWPFDocument document) {
        XWPFParagraph titleParagraph = document.createParagraph();    //新建一个标题段落对象（就是一段文字）
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);//样式居中
        XWPFRun titleFun = titleParagraph.createRun();    //创建文本对象

        //titleFun.setText(titleName); //设置标题的名字
        titleFun.setBold(true); //加粗
        titleFun.setColor("000000");//设置颜色
        titleFun.setFontSize(22);    //字体大小
        titleFun.setFontFamily("方正小标宋简体");//设置字体
        //...
        //titleFun.addBreak();    //换行  当前行切到下一行
    }
    /**
     * 设置表格格式
     * @param document
     * @param rows
     * @param cols
     * @Author Huangxiaocong 2018年12月16日
     */
    public void createTableParagraph(XWPFDocument document, int rows, int cols) {
        //xwpfHelperTable.createTable(xdoc, rowSize, cellSize, isSetColWidth, colWidths)
        //新建一个表格 名字为infoTable
        XWPFTable infoTable = document.createTable(rows, cols);
        xwpfHelperTable.setTableWidthAndHAlign(infoTable, "75000", STJc.CENTER);



        for(int col = 0; col < 15; col++){
            if(col==0||col==4||col==6||col==8||col==10){
                //合并行单元格
                xwpfHelperTable.mergeCellsHorizontal(infoTable, col, 1, 3);
            }else if(col==11||col==12||col==13||col==14){
                //合并行单元格
                xwpfHelperTable.mergeCellsHorizontal(infoTable, col, 1, 3);
            }
        }
        //xwpfHelperTable.mergeCellsVertically(infoTable, 0, 11, 14);
        //设置表格样式
        List<XWPFTableRow> rowList = infoTable.getRows();
        for(int i = 0; i < rowList.size(); i++) {
            XWPFTableRow infoTableRow = rowList.get(i);
            //设置行高
            CTTrPr trPr = rowList.get(i).getCtRow().addNewTrPr();
            CTHeight ht = trPr.addNewTrHeight();
            ht.setVal(BigInteger.valueOf(600));



            List<XWPFTableCell> cellList = infoTableRow.getTableCells();

            for(int j = 0; j < cellList.size(); j++) {
                XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
                //设置列宽
                CTTcPr tcpr = cellList.get(j).getCTTc().addNewTcPr();
                CTTblWidth cellw = tcpr.addNewTcW();
                cellw.setType(STTblWidth.DXA);
                cellw.setW(BigInteger.valueOf(2000));

                cellParagraph.setAlignment(ParagraphAlignment.CENTER);
                XWPFRun cellParagraphRun = cellParagraph.createRun();
                cellParagraphRun.setFontSize(14);

                CTShd ctshd = tcpr.addNewShd();
                ctshd.setColor("auto");
                ctshd.setVal(STShd.CLEAR);

                //cellParagraphRun.setFontFamily("仿宋_GB2312");
                if((j == 0 ||j==2)&&i!=11&&i!=12&&i!=13&&i!=14) {
                    cellParagraphRun.setFontFamily("楷体_GB2312");
                    cellParagraphRun.setBold(true);
                }else if((i==11||i==12||i==13||i==14)&&j==0){
                        cellParagraphRun.setFontFamily("楷体_GB2312");
                        cellParagraphRun.setBold(true);
                }else{
                    cellParagraphRun.setFontFamily("仿宋_GB2312");
                }
                /*
                设置表格背景颜色
                if(i%2==1){
                    ctshd.setFill("A7BFDE");
                }else{
                    ctshd.setFill("D3DFEE");
                }*/
            }
        }
        //设置表格高度
        xwpfHelperTable.setTableHeight(infoTable, 750, STVerticalJc.CENTER);
    }

    /**
     * 往表格中填充数据
     * @param dataList
     * @param document
     * @throws IOException
     * @Author Huangxiaocong 2018年12月16日
     */
    public void exportCheckWord(Map<String, Object> dataList, XWPFDocument document,String contextPath) throws IOException {
        XWPFParagraph paragraph = document.getParagraphArray(0);
        XWPFRun titleFun = paragraph.getRuns().get(0);
        titleFun.setText(String.valueOf(dataList.get("TITLE")));
        List<List<Object>> tableData = (List<List<Object>>) dataList.get("TABLEDATA");
        XWPFTable table = document.getTableArray(0);
        fillTableData(table, tableData,contextPath);
        //xwpfHelper.saveDocument(document, savePath);

    }
    /**
     * 往表格中填充数据
     * @param table
     * @param tableData
     * @Author Huangxiaocong 2018年12月16日
     */
     public void fillTableData(XWPFTable table, List<List<Object>> tableData,String contextPath) throws UnknownHostException {
         //表格的行的格子
        List<XWPFTableRow> rowList = table.getRows();
        //循环表格属于行的数据
        for(int i = 0; i < tableData.size(); i++) {
            //每一行的数据  i=行
            List<Object> list = tableData.get(i);
            //表格第i行的列的格子
            List<XWPFTableCell> cellList = rowList.get(i).getTableCells();
            //循环第i行里面列的数据
            for(int j = 0; j < list.size(); j++) {
                //文本类
                XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
                XWPFRun cellParagraphRun = cellParagraph.getRuns().get(0);
                //获取id当前服务器的ip地址
                InetAddress address = InetAddress.getLocalHost();

                if((i==11||i==12||i==13||i==14)&&j==1){
                    if(list.get(j)!=null&&!"".equals(list.get(j))){
                        xwpfHelper.appendExternalHyperlink(contextPath+list.get(j),"点击下载",cellParagraph);
                    }
                }else{
                    cellParagraphRun.setText(String.valueOf(list.get(j)));
                }
            }
        }
    }
}
